package com.test.rabbitmq_extension.Listener;

import com.rabbitmq.client.Channel;
import com.test.rabbitmq_extension.Constants.Constants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.io.UnsupportedEncodingException;

@Component
@Slf4j
public class AckListener {
    @RabbitListener(queues = Constants.ACK_QUEUE)
    public void listener(Message message, Channel channel) throws IOException {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            log.info("接收消息:" + new String(message.getBody(), "UTF-8") + "-------" + message.getMessageProperties().getDeliveryTag());
            channel.basicAck(deliveryTag,false);
        } catch (Exception e) {
            channel.basicNack(deliveryTag,false,false);
        }
    }
}
